// packageContract/pages/electronic-contract/electronic-contract.js

import {
    getBindCompanySupplier,
} from '../../../api/app'
import { contractSignRecordList,contractTemplateList,launchContract } from '../../api/contract'
import user from '../../../utils/user.js'
import {toast} from '../../../utils/util'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        tabList: [
            {
                title: '签署记录',
                num: '0'
            },
            {
                title: '合同模版',
                num: '0'
            }
        ],
        tabIndex:0,
        pageNum:1,
        pageSize:20,
        signSwitch:true,
        signList:[],
        templateNum:1,
        templateSize:20,
        templatSwitch:true,
        templateList:[],
        eleSignConsumeAmount:0,
        eleSignOverAmount:0,
        // 弹框里的
        enterpriseName:'',
        enterTemplate:'',
        tipSwitch:false,
        launchContract:false,
        tipList:[],
        tipEnterprise:true,
        tipEnterpriseList:[],
        enterpriseId:'',
        templateId:'',
        todaySignAmount:'0',
        unsignedAmount:'0',
        signingFailedRecordAmount:'0',
        labourCost:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        
    },
    onShow(){
        this.setData({
            signList:[],
            templateList:[],
            pageNum:1,
            templateNum:1,
            signSwitch:true,
            templatSwitch:true
        })
        user.waitLogin().then(() => {
            this.contractSignRecord()
            this.contractTemplate()
        })
    },
    onTab(event) {
        let { detail } = event
        this.setData({
            tabIndex:detail
        })
    },
    contractSignRecord(){
        contractSignRecordList({
            pageNum:this.data.pageNum,
            pageSize:this.data.pageSize
        }).then(res=>{
            console.log(res,'res')
            // ['data.personnelList['+event.currentTarget.dataset.index+'].status']: 2\
            if(!res.hasNextPage){
                this.setData({
                    signSwitch:false
                })
            }
            const add = []
            add.push(...this.data.signList)
            add.push(...res.list)
            this.setData({
                ['tabList['+0+'].num']:res.totalCount,
                eleSignConsumeAmount:res.eleSignConsumeAmount,
                eleSignOverAmount:res.eleSignOverAmount,
                signList:add,
                signingFailedRecordAmount:res.signingFailedRecordAmount,
                todaySignAmount:res.todaySignAmount,
                unsignedAmount:res.unsignedAmount
            })
        })
    },
    scrolltolower(){
        if(this.data.tabIndex === 0 && this.data.signSwitch){
            console.log('调用啦吗1')
            this.setData({
                pageNum:this.data.pageNum+=1
            })
            this.contractSignRecord()
        }else{
            console.log('调用啦吗2')
            return
        }

        if(this.data.tabIndex === 1 && this.data.templatSwitch){
            this.setData({
                templateNum:this.data.templateNum+=1
            })
            this.contractTemplate()
        }else{
            return
        }
    },
    contractTemplate(){
        contractTemplateList({
            pageNum:this.data.templateNum,
            pageSize:this.data.templateSize
        }).then(res=>{
            console.log(res,'resres')
            if(!res.hasNextPage){
                this.setData({
                    templatSwitch:false
                })
            }
            const add = []
            add.push(...this.data.templateList)
            add.push(...res.list)
            this.setData({
                ['tabList['+1+'].num']:res.totalCount,
                templateList:add
            })
        })
    },
    // 去合同详情
    onContractDetails(event){
        //  recordCount , unsignedCount 
        const { templateid } = event.currentTarget.dataset
        wx.navigateTo({
            // &back=' + 'home'
            url: '/packageContract/pages/contract-details/contract-details?templateId=' + templateid
        })
    },
    // 去签署详情
    onSignDetails(event){
        const { id } = event.currentTarget.dataset
        wx.navigateTo({
            url: '/packageContract/pages/sign-details/sign-details?id=' + id
        })
    },
    // 弹框方法
    onLaunchContract(){
        this.setData({
            launchContract:true
        })
        contractTemplateList({
            pageSize:1000
        }).then(res=>{
            this.setData({
                tipList:res.list
            },()=>{
                console.log(this.data.tipList,'tipList')
            })
        })
        getBindCompanySupplier({
            companyId:user.companyId,
        }).then(res=>{
            this.setData({
                tipEnterpriseList:res
            })
        })
    },
    // 点击空白关闭弹框
    cancel(){
        this.setData({
            launchContract:false
        })
    },
    // 企业获取焦点
    onNameInput(){
        if(!this.data.tipSwitch){
            this.setData({
                tipEnterprise:true
            })
        }
    },
    // 企业失去焦点
    enterpriseBlur(){
        this.setData({
            
        })
    },
    enterpriseTapEnter(event){
        const { name,id } = event.currentTarget.dataset
        console.log(id,'id')
        this.setData({
            enterpriseName:name,
            enterpriseId:id,
            tipEnterprise:false
        })
    },
    // 模板获取焦点
    onEnterInput(){
        this.setData({
            tipSwitch:true
        })
    },
    // 失去焦点
    templateBlur(){
        this.setData({
            // tipSwitch:false
        })
    },
    templateTapEnter(event){
        const { name,id } = event.currentTarget.dataset
        this.setData({
            enterTemplate:name,
            templateId:id,
            tipSwitch:false
        })
    },
    onGoEnterprise() {
        wx.navigateTo({
            url: '/pages/enterprise/enterprise',
        })
    },
    confirmLaunch(){
        if(!this.data.enterpriseName){
            toast('请选择企业')
            return
        }
        if(!this.data.enterTemplate){
            toast('请选择合同模板')
            return
        }
        // if(!this.data.labourCost){
        //     toast('请输入工价')
        //     return
        // }
        launchContract({
            serveCompanyId:this.data.enterpriseId,
            templateId:this.data.templateId,
            labourCost:this.data.labourCost
        }).then(res=>{
            this.setData({
                launchContract:false
            })
            this.setData({
                pageNum:1,
                signList:[],
                signSwitch:true,
                enterpriseName:'',
                enterTemplate:''
            })
            wx.navigateTo({
                url: '/packageContract/pages/sign-details/sign-details?id=' + res.id
            })
        })
    },
    

    // 弹框搜索企业
    searchEnterprise(event){
        const { value } = event.detail
        getBindCompanySupplier({
            companyId:user.companyId,
            serveCompanyName:value
        }).then(res=>{
            this.setData({
                tipEnterpriseList:res
            })
        })
    },

    // 跳转到共签署合同
    onCommonSign(event){
        // console.log(event.currentTarget.dataset,'event')
        const { type } = event.currentTarget.dataset
        wx.navigateTo({
            url: '/packageContract/pages/common-sign/common-sign?type=' + type
        })
    },

    onWagesInput(event){
        console.log(event.detail.value,'event')
        this.setData({
            labourCost:event.detail.value
        })
    }
})